package org.sherlock.plugins.revealers;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.Charset;
import java.util.ArrayList;

import org.sherlock.commons.model.RemoteFile;
import org.sherlock.commons.model.RemoteMusicFile;
import org.sherlock.commons.plugins.RevealerException;
import org.sherlock.commons.plugins.URLRevealer;

public class Revealer extends URLRevealer {

	@Override
	public ArrayList<RemoteFile> getURLs(URL url) throws RevealerException {
		ArrayList<RemoteFile> files = new ArrayList<RemoteFile>();
		String id = url.toString().substring(url.toString().lastIndexOf('-') + 1);
		try {
			String xmlCode = getContent(new URL("http://www.myspace.com/music/services/player?action=getSong&songId=" + id).openStream(), Charset.defaultCharset());
			String title = xmlCode.split("<title>")[1].split("</title>")[0];
			String rtmp = xmlCode.split("<rtmp>")[1].split("</rtmp>")[0];
			String artist = xmlCode.split("<artist name=\"")[1].split("\"")[0];
			files.add(new RemoteMusicFile(artist, "Unknown", title, new URI(rtmp)));
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// TODO Auto-generated method stub
		return files;
	}

}
